clear

set more off
set mem 100m


local EVENT  "majorinitiative1summit" /*majorinitiative1, majorinitiative2, majorinitiative1summit, or majorinitiative2alt*/
local thintrade    = 0 /*0: Do not deal with thin trading issues, 1 deal with  them using trade-to-trade returns*/
local controlevents = 1  /*Whether to include dummies for the corporate events occuring during the relevant periods*/
local controlindustry = 1 /*Whether to include a industrial index in the baseline model (improves the fit significanlty*/
local robust = 1 /*1: Use longest estimation window*/
local robust = 1 /*Use longest estimation window (There are 1-7 for the non-thin trade case only)*/
local estwindow 	= 180 /*Calender days*/
local evtwindow 	= 30 /*Calender days: this does not define the length of the window for the actual estimation, just for calling the file. I'm using a window of 10 in line 42*/

use Data_Event_Study_final_thin`thintrade'_est`estwindow'_evt`evtwindow'_control`controlevents'_ind`controlindustry'_`EVENT', replace


if "`EVENT'"=="majorinitiative1" | "`EVENT'"=="majorinitiative1summit" {
	drop if ticker=="GND" & eventyear==1996
}


if `thintrade'== 0 {
	local thin = ""
}
else if `thintrade'== 1 {
	local thin = "tt"
}



keep panel_id estimation_window event_window event_time AbnormalReturns`thin' AbnormalReturns`thin'd eventyear
drop if (estimation_window==. & event_window==.) | event_time==.



******W/ DIVIDENDS*******
sort panel_id event_time

by panel_id: gen cumAbnormalReturns`thin'd = sum(AbnormalReturns`thin'd)

egen Ndays = count(AbnormalReturns`thin'd), by (panel_id)

egen medAR = median(AbnormalReturns`thin'd), by(panel_id)

gen S_it = .

replace S_it = 1 if AbnormalReturns`thin'd > 0 & AbnormalReturns`thin'd<.
replace S_it = 0 if AbnormalReturns`thin'd <=0

gen cS_it = .

replace cS_it = 1 if cumAbnormalReturns`thin'd > 0 & AbnormalReturns`thin'd<.
replace cS_it = 0 if cumAbnormalReturns`thin'd <= 0


*MEAM DEVIATION & STANDARD DEVIATION

gen stdS_all = .
gen stdS_event = .

egen S_t_all 	= mean(S_it) if estimation_window==1, by(event_time)
egen S_t_event 	= mean(S_it) if estimation_window==1, by(eventyear event_time)

egen tmp= mean(S_it) if estimation_window==1, by(panel_id)
egen S_i	= max(tmp), by(panel_id)
drop tmp

egen p_all 	    = mean(S_i)
egen p_event 	= mean(S_i), by(eventyear)

egen W_all 	    = sum(S_it) if event_window==1, by(event_time)
egen W_event	= sum(S_it) if event_window==1, by(eventyear event_time)

egen w_all 	    = sum(cS_it) if event_window==1, by(event_time)
egen w_event	= sum(cS_it) if event_window==1, by(eventyear event_time)

egen N_all 	    	= count(S_i) , by(event_time)
egen  N_event    	= count(S_i) , by(eventyear event_time)

//Collapsing at the event-year event_day dimension

collapse S_t_*  p_* W_* w_*  N_* estimation_window event_window, by(eventyear event_time)

gen dW_all = W_all - N_all*p_all
gen dw_all = w_all - N_all*p_all

gen dW_event = W_event - N_event*p_event
gen dw_event = w_event - N_event*p_event

gen sd_W_all = (N_all*p_all*(1-p_all))^0.5
gen sd_W_event = (N_event*p_event*(1-p_event))^0.5

gen Z_all	= (W_all - N_all*p_all)/(N_all*p_all*(1-p_all))^0.5
gen cZ_all	= (w_all - N_all*p_all)/(N_all*p_all*(1-p_all))^0.5

gen Z_event	    = (W_event - N_event*p_event)/(N_event*p_event*(1-p_event))^0.5
gen cZ_event	= (w_event - N_event*p_event)/(N_event*p_event*(1-p_event))^0.5

sort eventyear event_time

by eventyear: gen cumdWevent = sum(dW_event) if event_window==1
by eventyear: gen cumdWall = sum(dW_all) if event_window==1


keep if event_window==1

by eventyear: gen T = _n if event_window==1

gen tmp1 = sd_W_event^2
gen tmp2 = sd_W_all^2

by eventyear: gen tmp3 = sum(tmp1) if event_window==1
by eventyear: gen tmp4 = sum(tmp2) if event_window==1

gen sd_cumdWevent = sqrt(tmp3)
gen sd_cumdWall      = sqrt(tmp4)


*SIGN TEST STATISTICS

gen ciu_event = cumdWevent + sd_cumdWevent*1.64
gen cil_event = cumdWevent - sd_cumdWevent*1.64

gen ciu_all = cumdWall + sd_cumdWall*1.64
gen cil_all = cumdWall - sd_cumdWall*1.64

gen pvalue_event = (1-normal(abs(Z_event)))*2
gen pvalue_all = (1-normal(abs(Z_all)))*2


sort eventyear event_time


*PLOT THESE WITH 90% CONFIDENCE INTERVAL BANDS AGAINST EVENT TIME
graph drop _all

twoway (line  cumdWevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1996, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean sign deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off)  name(sign_96_`EVENT')
gr save  Figure_6_B, replace 	

twoway (line  cumdWevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1999, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean sign deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(sign_99_`EVENT')
gr save Figure_6_C, replace 	

twoway (line  cumdWevent event_time, lwidth(thick) ) (line ciu_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==2005, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean sign deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(sign_05_`EVENT')
gr save Figure_6_D, replace

twoway (line  cumdWall event_time, lwidth(thick) ) (line ciu_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) ///
(line cil_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) if eventyear==2005, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative mean sign deviation, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name(sign_all_`EVENT')
gr save Figure_6_A, replace


keep eventyear event_time dW_event dW_all sd_W_event sd_W_all Z_all Z_event
foreach v of varlist dW_all sd_W_all Z_all {
	egen paso = min(`v'), by(event_time)
	replace `v' = paso
	qui drop paso
}

reshape wide dW_event sd_W_event Z_event, i(event_time) j(eventyear)

saveold  Table_5_B, replace

